/*
 * FILENAME
 *     User.java
 *
 * FILE LOCATION
 *     $Source$
 *
 * VERSION
 *     $Id$
 *         @version       $Revision$
 *         Check-Out Tag: $Name$
 *         Locked By:     $Lockers$
 *
 * FORMATTING NOTES
 *     * Lines should be limited to 78 characters.
 *     * Files should contain no tabs.
 *     * Indent code using four-character increments.
 *
 */

package edu.haiu.training.data;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
//
// IMPORTS
// NOTE: Import specific classes without using wildcards.
//
@Entity
@Table(name = "user")
public class User extends BaseEntity {
	@Column(nullable = false, unique = true)
	private String username;

	@Column(nullable = false)
	private int id1;
	private String password;
	private String confirmpassword;
	private String firstName;
	private String lastName;
	private String address;
	private Date birthday;
	private boolean administrator;
	private UserState state;
	private static final String username1 ="root";
	private static final String password1 ="123456";
	private static final String database1 ="jdbc:mysql://localhost:3306/mydb";
	
	public User() {
	}
	
	public int getId1() {
		return id1;
	}

	public void setId1(int id1) {
		this.id1 = id1;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public String getConfirmpassword() {
		return confirmpassword;
	}

	public void setConfirmpassword(String confirmpassword) {
		this.confirmpassword = confirmpassword;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public Date getBirthday() {
		return birthday;
	}
//	public void setBirthday(Date birthday1){
//		this.birthday= birthday1;
//	}
	public void setBirthday(String birthday) throws ParseException {
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		this.birthday = (Date)formatter.parse(birthday);	
	}
	
	public boolean isAdministrator() {
		return administrator;
	}

	public void setAdministrator(boolean administrator) {
		this.administrator = administrator;
	}

	public UserState getState() {
		return state;
	}

	public void setState(final UserState newState) {
		this.state = newState;
	}
	
	public String execute(){
		return "success";
	}
//	public String execute2() throws Exception{
//		Connection connection = DriverManager.getConnection(database1,username1,password1);
//		String qs= "INSERT INTO mydb.user(`Username`, `Password`, `FirstName`, `LastName`, `Address`, `Birthday`, `Administrator`,`State`) VALUES(?, ?, ?, ?, ?, ?, ?, ?)";
//		PreparedStatement st= connection.prepareStatement(qs);
//		List<User> user = new ArrayList<User>();
//		if (password.equals(confirmpassword)) {
//			for(User u: user){
//				if(username!= u.getUsername()){
//					st.setString(1, username);
//					st.setString(2, password);
//					st.setString(3, firstName);
//					st.setString(4, lastName);
//					st.setString(5, address);
//					st.setDate(6,new java.sql.Date(birthday.getTime()));
//					st.setBoolean(7, administrator);
//					st.setInt(8, getState().ordinal());
//					st.executeUpdate();
//					connection.close();
//				}
//			}
//			return "success";
//		}
//		else
//			return "unsuccess";
//	}
	public String execute2() throws Exception{
		Connection connection = DriverManager.getConnection(database1,username1,password1);
		String qs= "INSERT INTO mydb.user(`Username`, `Password`, `FirstName`, `LastName`, `Address`, `Birthday`, `Administrator`,`State`) VALUES(?, ?, ?, ?, ?, ?, ?, ?)";
		PreparedStatement st= connection.prepareStatement(qs);
		if (password.equals(confirmpassword)) {
			st.setString(1, username);
			st.setString(2, password);
			st.setString(3, firstName);
			st.setString(4, lastName);
			st.setString(5, address);
			st.setDate(6,new java.sql.Date(birthday.getTime()));
			st.setBoolean(7, administrator);
			st.setInt(8, getState().ordinal());
			st.executeUpdate();
			connection.close();
			return "success";
		}
		else
			return "unsuccess";
	}
	public void deleteUser(int idUser) throws Exception{
		Connection connection = DriverManager.getConnection(database1,username1,password1);
		PreparedStatement ps = connection.prepareStatement("delete from mydb.user where Id=?");
		ps.setInt(1, idUser);
		ps.executeUpdate();
		connection.close();
	}
	public void editUser(int idUser, String username, String password, String firstName, String lastName, String address, Date birthday, boolean administrator, UserState state ) throws Exception{
		Connection connection = DriverManager.getConnection(database1,username1,password1);
		PreparedStatement ps = connection.prepareStatement("update mydb.user set Username=? Password=? FirstName=? LastName=? Address=? Birthday=? Administrator=? State=? "+"where Id=" + idUser);//+String.valueOf(id1)
		ps.setString(2, username);
		ps.setString(3, password);
		ps.setString(4, firstName);
		ps.setString(5, lastName);
		ps.setString(6, address);
		ps.setDate(7,new java.sql.Date(birthday.getTime()));
		ps.setBoolean(8, administrator);
		ps.setInt(9, getState().ordinal());
		ps.executeUpdate();
		connection.close();
		
	}
	public String edit(int idUser)throws Exception{
		Connection connection = DriverManager.getConnection(database1,username1,password1);
		PreparedStatement ps = connection.prepareStatement("select* from mydb.user where Id=" + idUser );
		
		ResultSet rs = ps.executeQuery();
		while(rs.next()){	
			 User user = new User();
			 user.setUsername(rs.getString("Username"));
			 user.setPassword(rs.getString("Password"));
			 user.setFirstName(rs.getString("FirstName"));
			 user.setLastName(rs.getString("LastName"));
			 user.setAddress(rs.getString("Address"));
			 user.setBirthday(String.valueOf(new java.sql.Date(rs.getDate("Birthday").getTime())));
			 user.setAdministrator(rs.getBoolean("Administrator"));
			 user.setState(UserState.values()[rs.getInt("State")]);
			 username=user.getUsername();
			 password=user.getPassword();
			 firstName=user.getFirstName();
			 lastName=user.getLastName();
			 address=user.getAddress();
			 birthday=user.getBirthday();
			 state=user.getState();
		}
		connection.close();
		return "success";
	}	
	
	public String showEdit() throws Exception{
		User user = new User();
		try {
			HttpServletRequest rq = ServletActionContext.getRequest();
			int idUser = Integer.parseInt(rq.getParameter("id2"));
			user.edit(idUser);
		} catch(NumberFormatException e) {
			e.printStackTrace();
		}
		return "success";
	}
//	public  String dividePage() throws Exception{
//		Connection connection = DriverManager.getConnection(database1,username1,password1);
//		String query= "select count(*) from mydb.user";
//		PreparedStatement ps = connection.prepareStatement(query);
//		ResultSet rs = ps.executeQuery(query);
//		int rowNumber=0;
//		while(rs.next()){
//			rowNumber = rs.getInt(1);
//		}
//		int idP=0;
//		String variableNumber = ServletActionContext.getRequest().getParameter("idP"); 
//		if(variableNumber != null){
//			if(variableNumber.equals(0)){
//				idP=0;
//			}
//			else
//				idP=Integer.parseInt(variableNumber)-1;
//		}
//	return "success";
//	}
//	
}
